%%%-------------------------------------------------------------------
%% @doc metronome_beat public API
%% @end
%%%-------------------------------------------------------------------

-module(metronome_beat).
-behaviour(gen_server).


%% --------------------------------------------------------------------
%% External exports
%% --------------------------------------------------------------------
-export([start_link/1]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).


%% ====================================================================
%% External functions
%% ====================================================================
start_link(BeatTimer) ->
    gen_server:start_link({local, ?MODULE}, ?MODULE, [BeatTimer], []).


%% ====================================================================
init([BeatTimer]) ->
    {ok, BeatTimer, BeatTimer}.

%% --------------------------------------------------------------------
handle_call(_Info, _From, BeatTimer) ->
    {reply, ok, BeatTimer, BeatTimer}.

%% --------------------------------------------------------------------
%% 改变节拍间隔时间，不建议使用
handle_cast({beat_timer, BeatTimer}, _BeatTimer) ->
    {noreply, BeatTimer, BeatTimer};

handle_cast(_Info, BeatTimer) ->
    {noreply, BeatTimer, BeatTimer}.

%% --------------------------------------------------------------------
handle_info(timeout, BeatTimer) ->
    gen_server:cast(metronome_subscribe, beat_timer),
    {noreply, BeatTimer, BeatTimer};

handle_info(_Info, BeatTimer) ->
    {noreply, BeatTimer, BeatTimer}.

%% --------------------------------------------------------------------
terminate(_Reason, _Status) ->
    ok.

%% --------------------------------------------------------------------
code_change(_oldvsn, Status, _extra) ->
    {ok, Status}.



